//
//  HYLogin.m
//  child
//
//  Created by lijianguomac on 13-9-24.
//  Copyright (c) 2013年 lijianguomac. All rights reserved.
//

#import "HYLogin.h"
#import "HYAppDelegate.h"
#import "HYHome.h"

@interface HYLogin (){
    BOOL isSelected;
}


@end

@implementation HYLogin

@synthesize tabBarController = _tabBarController;

@synthesize tfUsername,tfPassword;

-(void)dealloc{
    [super dealloc];
//    delegate = nil;
    [tfUsername release];
    [tfPassword release];
}

- (id)init{
    self = [super init];
    if(self){
        [self myInit];
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        [self myInit];
    }
    return self;
}

-(void)myInit{
//    [self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.png"]]];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.png"]]];
    
    CGRect frame = CGRectMake(230, 8, 80, 30);
    UILabel *lblVersion = [[UILabel alloc] initWithFrame:frame];
    lblVersion.backgroundColor = [UIColor clearColor];
    [lblVersion setFont:[UIFont systemFontOfSize:11]];
    [lblVersion setText:[NSString stringWithFormat:@"版本号:%@",[Tool appVersion]]];
    [self.view addSubview:lblVersion];
    [lblVersion release];
    
    frame = CGRectMake(20, 40, 280, 30);
    tfUsername = [[UITextField alloc] initWithFrame:frame];
    [tfUsername setBorderStyle:UITextBorderStyleRoundedRect];
    [tfUsername setFont:[UIFont systemFontOfSize:15]];
    [tfUsername setReturnKeyType:UIReturnKeyNext];
    [tfUsername addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEndOnExit];
    tfUsername.placeholder = @"输入帐号";
    [self.view addSubview:tfUsername];
    
    frame = CGRectMake(20, 90, 280, 30);
    tfPassword = [[UITextField alloc] initWithFrame:frame];
    [tfPassword setBorderStyle:UITextBorderStyleRoundedRect];
    [tfPassword setFont:[UIFont systemFontOfSize:15]];
    [tfPassword setSecureTextEntry:YES];
    [tfPassword setReturnKeyType:UIReturnKeyDone];
    [tfPassword addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEndOnExit];
    tfPassword.placeholder = @"输入密码";
    [self.view addSubview:tfPassword];

    UIButton *btnPassword = [UIButton buttonWithType:UIButtonTypeCustom];
    btnPassword.frame = CGRectMake(20, 143, 18, 18);
    [btnPassword addTarget:self action:@selector(clickBtnPassword:) forControlEvents:UIControlEventTouchUpInside];
    [btnPassword setBackgroundImage:[UIImage imageNamed:@"checkbox_nor.png"] forState:UIControlStateNormal];
    [self.view addSubview:btnPassword];
    
    frame = CGRectMake(40, 145, 70, 15);
    UILabel *lblPassword = [[UILabel alloc] initWithFrame:frame];
    lblPassword.backgroundColor = [UIColor clearColor];
    [lblPassword setFont:[UIFont systemFontOfSize:14]];
    [lblPassword setText:@"记住密码"];
    [self.view addSubview:lblPassword];
    [lblPassword release];
    
    frame = CGRectMake(185, 140, 95, 30);
    UIButton *btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLogin.frame = frame;
    [btnLogin addTarget:self action:@selector(clickBtnLogin:) forControlEvents:UIControlEventTouchUpInside];
    [btnLogin setBackgroundImage:[UIImage imageNamed:@"login_btn.png"] forState:UIControlStateNormal];
    [self.view addSubview:btnLogin];
}

-(void)textEnd:(id)sender{
    UITextField *tf = (UITextField *)sender;
    if(tf==tfUsername){
        [tfPassword becomeFirstResponder];
    }else{
        [self clickBtnLogin:nil];
    }
    
}

-(void)clickBtnPassword:(id)sender{
    UIButton *btnPassword = (UIButton *)sender;
    if(isSelected){
        [btnPassword setBackgroundImage:[UIImage imageNamed:@"checkbox_nor.png"] forState:UIControlStateNormal];
    }else{
        
        [btnPassword setBackgroundImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateNormal];
    }
    
    isSelected=!isSelected;
}

-(void)clickBtnLogin:(id)sender{
//    [delegate goHome];
//    [HYAppDelegate goHome];
//    [self.navigationController popViewControllerAnimated:YES];
    
    //首页
    HYHome *home = [[HYHome alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController * homeNav = [[UINavigationController alloc] initWithRootViewController:home];
    [home release];
    //设置头部背景图片
    [homeNav.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_header.png"] forBarMetrics:UIBarMetricsDefault];
    
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate = self;
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:
                                             homeNav,
                                             nil];
    
    //设置tabBar背景图片
    self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"bg_footer.png"];
    [homeNav release];
//    [UIApplication sharedApplication]
//    HYAppDelegate *delegate = (HYAppDelegate *)[[UIApplication sharedApplication] delegate];
//    delegate.window.rootViewController = self.tabBarController;
//    [delegate.window makeKeyAndVisible];
    
    [self presentModalViewController:self.tabBarController animated:YES];
    [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
    if ( IOS7_OR_LATER )
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        
    }
#endif
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [tfUsername becomeFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
